package com.jast.business.controller;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jast.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jast.common.annotation.Log;
import com.jast.common.enums.BusinessType;
import com.jast.business.domain.BigdataHbaseKv;
import com.jast.business.service.IBigdataHbaseKvService;
import com.jast.common.core.controller.BaseController;
import com.jast.common.core.domain.AjaxResult;
import com.jast.common.utils.poi.ExcelUtil;
import com.jast.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * Hbase监控指标最新值Controller
 * 
 * @author Jast
 * @date 2023-02-02
 */
@Controller
@RequestMapping("/business/hbase")
public class BigdataHbaseKvController extends BaseController
{
    private String prefix = "business/hbase";

    @Autowired
    private IBigdataHbaseKvService bigdataHbaseKvService;

    @RequiresPermissions("business:hbase:view")
    @GetMapping()
    public String hbase()
    {
        return prefix + "/hbase";
    }

    /**
     * 查询Hbase监控指标最新值列表
     */
    @RequiresPermissions("business:hbase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BigdataHbaseKv bigdataHbaseKv)
    {
        startPage();
        List<BigdataHbaseKv> list = bigdataHbaseKvService.selectBigdataHbaseKvList(bigdataHbaseKv);
        return getDataTable(list);
    }

    /**
     * 导出Hbase监控指标最新值列表
     */
    @RequiresPermissions("business:hbase:export")
    @Log(title = "Hbase监控指标最新值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BigdataHbaseKv bigdataHbaseKv)
    {
        List<BigdataHbaseKv> list = bigdataHbaseKvService.selectBigdataHbaseKvList(bigdataHbaseKv);
        ExcelUtil<BigdataHbaseKv> util = new ExcelUtil<BigdataHbaseKv>(BigdataHbaseKv.class);
        return util.exportExcel(list, "Hbase监控指标最新值数据");
    }

    /**
     * 新增Hbase监控指标最新值
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存Hbase监控指标最新值
     */
    @RequiresPermissions("business:hbase:add")
    @Log(title = "Hbase监控指标最新值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BigdataHbaseKv bigdataHbaseKv)
    {
        return toAjax(bigdataHbaseKvService.insertBigdataHbaseKv(bigdataHbaseKv));
    }


    /**
     * 更新/新增Hbase监控指标最新值
     */
    @Log(title = "Hbase监控指标最新值", businessType = BusinessType.UPDATE)
    @PostMapping("/upsert")
    @ResponseBody
    public AjaxResult upsert(BigdataHbaseKv bigdataHbaseKv)
    {
        return toAjax(bigdataHbaseKvService.upsertBigdataHbaseKv(bigdataHbaseKv));
    }

    /**
     * 修改Hbase监控指标最新值
     */
    @RequiresPermissions("business:hbase:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BigdataHbaseKv bigdataHbaseKv = bigdataHbaseKvService.selectBigdataHbaseKvById(id);
        mmap.put("bigdataHbaseKv", bigdataHbaseKv);
        return prefix + "/edit";
    }

    /**
     * 修改保存Hbase监控指标最新值
     */
    @RequiresPermissions("business:hbase:edit")
    @Log(title = "Hbase监控指标最新值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BigdataHbaseKv bigdataHbaseKv)
    {
        return toAjax(bigdataHbaseKvService.updateBigdataHbaseKv(bigdataHbaseKv));
    }

    /**
     * 删除Hbase监控指标最新值
     */
    @RequiresPermissions("business:hbase:remove")
    @Log(title = "Hbase监控指标最新值", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bigdataHbaseKvService.deleteBigdataHbaseKvByIds(ids));
    }


    @GetMapping("/monitor")
    public String redis(ModelMap modelMap, HttpServletRequest request)
    {
        return "bigdata/hbase/hbase";
    }

    /**
     * 查询Hbase监控指标最新值列表
     */
    @PostMapping("/getBasicInfo")
    @ResponseBody
    public AjaxResult getBasicInfo(BigdataHbaseKv bigdataHbaseKv)
    {
        JSONObject jsonObject = new JSONObject();
        List<BigdataHbaseKv> list = bigdataHbaseKvService.selectBigdataHbaseKvList(bigdataHbaseKv);
        System.out.println("size;"+list.size());
        for (BigdataHbaseKv hbaseKv : list) {
            if(hbaseKv.getHtype().equals("m")){
                switch (hbaseKv.getHkey()){
                    case "numDeadRegionServers":
                        jsonObject.put("mNumDeadRegionServers",hbaseKv.getHvalue());
                        break;
                    case "numRegionServers":
                        jsonObject.put("mNumRegionServers",hbaseKv.getHvalue());
                        break;
                    case "averageLoad":
                        jsonObject.put("mAverageLoad",hbaseKv.getHvalue());
                        break;
                    case "ritCount":
                        jsonObject.put("mRitCount",hbaseKv.getHvalue());
                        break;
                    case "ritCountOverThreshold":
                        jsonObject.put("mRitCountOverThreshold",hbaseKv.getHvalue());
                        break;
                    case "ritOldestAge":
                        jsonObject.put("mRitOldestAge",hbaseKv.getHvalue());
                        break;
                    case "balancerCluster_num_ops":
                        jsonObject.put("mBalancerCluster_num_ops",hbaseKv.getHvalue());
                        break;
                }
            }
        }

        System.out.println("查询参数:"+ bigdataHbaseKv.getCluster());
        System.out.println(JSON.toJSONString(success(jsonObject)));
        return success(jsonObject);
    }
}
